import {
RIG_OPERATOR_CHART_VERSION
} from "../../../src/constants/versions";
import {
RIG_PLATFORM_CHART_VERSION
} from "../../../src/constants/versions";
import CodeBlock from '@theme/CodeBlock';
import ThemedImage from "@theme/ThemedImage";
import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Setting up Rig on EKS on Amazon Web Services

## Prerequisites
This guide assumes you have a running [EKS](https://aws.amazon.com/eks/) cluster with the [AWS Load Balancer Controller](https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.8/) installed. [This guide](https://docs.aws.amazon.com/eks/latest/userguide/quickstart.html) by AWS shows how to setup a simple cluster and sample application with the Load Balancer Controller installed as well.

Besides just having an EKS cluster up and running, your `kubectl` should have its current context pointing to your cluster. [Here](https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html) you can see how to get that.

## Installing the Rig Operator

We install the Rig Operator using Helm. We need one piece of AWS specific configuration to make Ingress work such that your Capsules and Rig Platform is reachable on the Internet. AWS EKS has their own Ingress controller, the Amazon Load Balancer (ALB) Controller mentioned in *Prerequisites*. The Rig Operator needs to be instructed how it generates Ingress resources such that they fit with the ALB. This we do through our [Routes plugin](/operator-manual/setup-guide/operator/networking). The following Helm values configure it to work with the ALB.

```yaml title="Helm values - Operator"
config:
  pipeline:
    routesStep:
      plugin: rigdev.ingress_routes
      config: |
        ingressClassName: alb
		# The ALB handles TLS outside the cluster
        disableTLS: true
        annotations:
          alb.ingress.kubernetes.io/scheme: internet-facing
```
This makes all Ingress resources generated by the Rig Operator be an ALB type Ingress which exposes its host as internet facing, making it reachable outside the cluster.

Save the values file as `operator.yaml`
<CodeBlock language="bash">
{`helm upgrade --install rig-operator rig-operator \\
  --repo https://charts.rig.dev \\
  --version ${RIG_OPERATOR_CHART_VERSION} \\
  --namespace rig-system \\
  --create-namespace \\
  -f operator.yaml
`}
</CodeBlock>


## Installing the Rig Platform

We also install the Rig Platform using Helm. The Rig Platform depends on a PostgreSQL database. You can choose to connect your own, managed PostgreSQL (recommended for production setups) or spin up a small, in-cluster one (should only be for development setups).
Besides that, enabling `ingress` will spin up an Ingress resource which will receive a host onto which you can access the Platform.

```yaml title=Helm values - Platform""
ingress:
  host: ""
  enabled: true

# Uncomment if you want to use the small, in-cluster development database
# postgres:
#   enabled: true
## AWS specific storage class
#   storage:
#     className: ebs-sc

# Uncomment if you want to use your own managed database
#  client:
#    postgres:
#      database: "rig"  ## change if different database is used.
#      host: <host or ip>
#      user: <db user>
#      password: <db password>
#      # insecure: false  ## set only if DB connection is insecure
```
Save the config as `platform.yaml` and run

<CodeBlock language="bash">
{`helm upgrade --install rig-platform rig-platform \\
  --repo https://charts.rig.dev \\
  --version ${RIG_PLATFORM_CHART_VERSION} \\
  --namespace rig-system \\
  --create-namespace \\
  -f platform.yaml
`}
</CodeBlock>

After installing the platform, running
```
kubectl get ingress -n rig-system
```
should show the Ingress resource associated to the Rig Platform
```
NAME                        CLASS   HOSTS   ADDRESS                                      PORTS   AGE
rig-platform-rig-platform   alb     *       k8s-rigsyste-rigplatf- .... .amazonaws.com   80      81m
```
After a few minutes the adress should be available and take you to the Rig Platform start page.

![Rig Platform start page](/img/guides/landingpage.png)

To get accesss to the Platform you also need a Rig account which you can setup by running
```
kubectl exec -it -n rig-system deploy/rig-platform -- rig-admin init
```
after which you can log in on the Dashboard using the account you just created.


## Creating a Capsule with a public host
Now that we have the Platform up and running, we can create our first Capsule and give it a publicly reachable host. We will do this through the Dashboard.

<Tabs>
  <TabItem value="step1" label="1">
    <ThemedImage
      alt="1"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step1.png",
        dark: "/img/guides/aws/step1.png",
      }}
    />
	<span style={{color: "white"}}>Click **Create Project** and create a new project.</span>
  </TabItem>
  <TabItem value="step2" label="2">
    <ThemedImage
      alt="2"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step3.png",
        dark: "/img/guides/aws/step3.png",
      }}
    />
	<span style={{color: "white"}}>Click **Create Capsule** and create a new capsule.</span>
  </TabItem>
  <TabItem value="step3" label="3">
    <ThemedImage
      alt="3"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step4.png",
        dark: "/img/guides/aws/step4.png",
      }}
    />
	<span style={{color: "white"}}>Click on the capsule, then on the **prod** environment.</span>
  </TabItem>
  <TabItem value="step4" label="4">
    <ThemedImage
      alt="4"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step5.png",
        dark: "/img/guides/aws/step5.png",
      }}
    />
	<span style={{color: "white"}}>From the overview page of the Capsule, click **Configure**.</span>
  </TabItem>
  <TabItem value="step5" label="5">
    <ThemedImage
      alt="5"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step6.png",
        dark: "/img/guides/aws/step6.png",
      }}
    />
	<span style={{color: "white"}}>Click on **Images** and type in **nginx** to deploy an **nginx** container.</span>
  </TabItem>
  <TabItem value="step6" label="6">
    <ThemedImage
      alt="6"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step7.png",
        dark: "/img/guides/aws/step7.png",
      }}
    />
	<span style={{color: "white"}}>Click on **Network**, add a new interface and give it a name and expose port 80 (which nginx listens on).</span>
  </TabItem>
  <TabItem value="step7" label="7">
    <ThemedImage
      alt="7"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step8.png",
        dark: "/img/guides/aws/step8.png",
      }}
    />
	<span style={{color: "white"}}>Click **Add host** to make the interface publicly exposed. The host should have an empty configuration. Then click **Deploy**.</span> 
  </TabItem>
  <TabItem value="step8" label="8">
    <ThemedImage
      alt="8"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step9.png",
        dark: "/img/guides/aws/step9.png",
      }}
    />
	<span style={{color: "white"}}>After deploying, wait a few seconds until the rollout shows **All instances are running**.</span>
  </TabItem>
  <TabItem value="step9" label="9">
    <ThemedImage
      alt="9"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step10.png",
        dark: "/img/guides/aws/step10.png",
      }}
    />
	<span style={{color: "white"}}>Click on the row just below the **http mycapsule:80** header under **Network Interfaces**. This shows a hostname under **Properties**. Copy this address.</span>
  </TabItem>
  <TabItem value="step10" label="10">
    <ThemedImage
      alt="10"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/step11.png",
        dark: "/img/guides/aws/step11.png",
      }}
    />
	<span style={{color: "white"}}>After a few minutes the address should be working and you can see the default nginx landing page.</span>
  </TabItem>
</Tabs>

You now have a basic setup on AWS EKS with Rig running and utilizing the Amazon Load Balancer. From the AWS console you can see the loadbalancers created.

<Tabs>
  <TabItem value="aws1" label="1">
    <ThemedImage
      alt="1"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/aws1.png",
        dark: "/img/guides/aws/aws1.png",
      }}
    />
	<span style={{color: "white"}}>From the AWS console, search for **EC2**</span>
  </TabItem>
  <TabItem value="aws2" label="2">
    <ThemedImage
      alt="2"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/aws2.png",
        dark: "/img/guides/aws/aws2.png",
      }}
    />
	<span style={{color: "white"}}>From the EC2 dashboard, click **Load Balancers**</span>
  </TabItem>
  <TabItem value="aws3" label="3">
    <ThemedImage
      alt="3"
      customProps={{
        zoom: true,
      }}
      sources={{
        light: "/img/guides/aws/aws3.png",
        dark: "/img/guides/aws/aws3.png",
      }}
    />
	<span style={{color: "white"}}>Then you should see a table with the load balancers for your cluster. In this case we have one for the Rig Dashboard and one for the Capsule.</span>
  </TabItem>
</Tabs>
